package com.yangguang.huahui.ui.activitys;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Handler;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.yangguang.huahui.R;
import com.yangguang.huahui.app.App;
import com.yangguang.huahui.entity.DataInfo;
import com.yangguang.huahui.entity.HttpResult;
import com.yangguang.huahui.entity.Login;
import com.yangguang.huahui.mvp.presenter.RegisterPresenter;
import com.yangguang.huahui.mvp.view.RegisterView;
import com.yangguang.huahui.utils.CommonUtil;
import com.yangguang.huahui.utils.CryptoUtil;
import com.yangguang.huahui.utils.DialogUtil;
import com.yangguang.huahui.utils.SharedPreferenceUtil;

import butterknife.BindView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import butterknife.OnTextChanged;

public class RegisterActivity extends BaseMvpActivity<RegisterView, RegisterPresenter> implements RegisterView{
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.et_password)
    EditText et_password;
    @BindView(R.id.et_password_ag)
    EditText et_password_ag;
    @BindView(R.id.et_phone)
    EditText et_phone;
    @BindView(R.id.et_code)
    EditText et_code;
    @BindView(R.id.tv_code)
    TextView tv_code;//获取登录验证码
    @BindView(R.id.btn_save)
    Button btn_save;//底下蓝色按钮

    private Dialog dialog;

    @OnClick({R.id.iv_back, R.id.iv_close, R.id.tv_code, R.id.btn_save, R.id.mLL_login, R.id.iv_look, R.id.iv_look_1})
    void onClick(View v){
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_close:
                et_phone.setText("");
                break;
            case R.id.mLL_login:
                finish();
                break;
            case R.id.tv_code:
                mPresenter.getCode(et_phone.getText().toString());
                break;
            case R.id.btn_save:
                register();
                break;
            case R.id.iv_look:
                if (et_password.getInputType() == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD){
                    et_password.setInputType(InputType.TYPE_CLASS_TEXT
                            | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }else {
                    et_password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                }
                break;
            case R.id.iv_look_1:
                if (et_password_ag.getInputType() == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD){
                    et_password_ag.setInputType(InputType.TYPE_CLASS_TEXT
                            | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }else {
                    et_password_ag.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                }
                break;
        }
    }

    @OnTextChanged(R.id.et_phone)
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (s.length() == 11){
            tv_code.setEnabled(true);
            tv_code.setBackgroundResource(R.drawable.tv_get_code);
        }else {
            tv_code.setEnabled(false);
            tv_code.setBackgroundResource(R.drawable.tv_get_code_gray);
        }
    }

    @OnCheckedChanged(R.id.cb_agree)
    void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.cb_agree:
                if (isChecked) {
                    btn_save.setClickable(true);
                    btn_save.setEnabled(true);
                    btn_save.setBackgroundResource(R.drawable.btn_login_bg);
                } else {
                    btn_save.setClickable(false);
                    btn_save.setEnabled(false);
                    btn_save.setBackgroundResource(R.drawable.btn_login_gray);
                }
                break;
        }
    }

    @Override
    protected int initLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        App.getInstance().addActivity((Activity) mContext);
        tv_title.setText("注册");
    }

    @Override
    protected RegisterPresenter initPresenter() {
        return new RegisterPresenter();
    }

    @Override
    protected void fetchData() {

    }

    @Override
    public void onCode(HttpResult httpResult) {
        et_code.setText(httpResult.getCode());
        startThread();
    }

    @Override
    public void onRegister(Login login) {
        dialog.dismiss();
        CommonUtil.myToastA(mContext, login.getMsg());
        if (login.getStatus() == 200) {
            SharedPreferenceUtil.getInstance(mContext).putString(SharedPreferenceUtil.TOKEN, login.getInfo().getUid());
            SharedPreferenceUtil.getInstance(mContext).putString(SharedPreferenceUtil.AVATARS, login.getInfo().getAvatar());
            SharedPreferenceUtil.getInstance(mContext).putString(SharedPreferenceUtil.PHONE,
                    et_phone.getText().toString());
            SharedPreferenceUtil.getInstance(mContext).putString(SharedPreferenceUtil.PASSWORD,
                    CryptoUtil.md5(et_password.getText().toString()));
            startActivity(new Intent(mContext, RegisterActivity.class));
            finish();
        }
    }

    @Override
    public void onError() {
        dialog.dismiss();
    }

    private void register(){
        if (CommonUtil.isNull(et_phone.getText().toString()) ||
                et_phone.getText().toString().length() != 11){
            CommonUtil.myToastA(mContext, "请正确输入手机号");
        }else if (CommonUtil.isNull(et_code.getText().toString())){
            CommonUtil.myToastA(mContext, "请输入验证码");
        } else if (!CommonUtil.isNull(et_password.getText().toString()) &&
                !et_password.getText().toString().equals(et_password_ag.getText().toString())) {
            CommonUtil.myToastA(mContext, "请输入相同密码");
        }else {
            dialog = DialogUtil.showProgressDialog(mContext, "注册中...");
            mPresenter.register(et_phone.getText().toString(), et_code.getText().toString(), CryptoUtil.md5(et_password.getText().toString()),
                    CommonUtil.getIMEI(mContext), CommonUtil.getModel(), "Android", CommonUtil.getPhoneVersion(),
                    DataInfo.LAT, DataInfo.LNG);
        }
    }

    private void startThread() {
        new Thread() {
            public void run() {
                for (int i = 60; i > 0; i--) {
                    try {
                        countDownHandler.obtainMessage(1001, i + "").sendToTarget();
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                countDownHandler.sendEmptyMessage(1002);
            }
        }.start();
    }
    /**
     * 获取验证码倒计时
     */
    private Handler countDownHandler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {

            switch (msg.what) {
                case 1001:
                    String response = (String) msg.obj;
                    tv_code.setText("重新获取(" + response + "s)");
//                    SmsContent content = new SmsContent(mActivity, new Handler(), et_code);
//                    // 注册短信变化监听
//                    mActivity.getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, content);
                    break;
                case 1002:
                    // 恢复获取验证码按钮
                    tv_code.setText("获取验证码");
                    tv_code.setClickable(true);
                    tv_code.setBackgroundResource(R.drawable.tv_get_code);
                    break;
            }
        }
    };
}
